﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;
using Cobainsoft.Windows.Forms;
using System.Diagnostics;

namespace shelfManagement.UI.Archive
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.txtFontBar.Text = Global.QianZhui.Trim();
            this.txtBakBar.Text = Global.HouZhui.Trim();
        }
        private int ii = 0;
        private int bx = 0;
        private int by = 0;
        private int lx = 250;
        private int ly = 100;
        private int barData = 1;
        private string barFont = "";
        private string barBack = "";
        private int barLie = 1;
        private int barNum = 1;
        private int startI = 0;
        PrintDocument pd = new PrintDocument();

        private void comType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                barcodeControl2.BarcodeType = GetBarcodeType(comType.Text);
                Invalidate();
            }
            catch
            {
                MessageBox.Show("条码制式不支持！请重选", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        BarcodeType GetBarcodeType(string cType)
        {
            BarcodeType bt = BarcodeType.CODE128A;
            switch (cType.ToUpper())
            {
                case "CODE128A":
                    bt = BarcodeType.CODE128A;
                    break;
                case "C2OF5":
                    bt = BarcodeType.C2OF5;
                    break;
                case "CODABAR":
                    bt = BarcodeType.CODABAR;
                    break;
                case "CODE128B":
                    bt = BarcodeType.CODE128B;
                    break;
                case "CODE128C":
                    bt = BarcodeType.CODE128C;
                    break;
                case "CODE39":
                    bt = BarcodeType.CODE39;
                    break;
                case "CODE39CHECK":
                    bt = BarcodeType.CODE39CHECK;
                    break;
                case "CODE93":
                    bt = BarcodeType.CODE93;
                    break;
                case "EAN128A":
                    bt = BarcodeType.EAN128A;
                    break;
                case "EAN128B":
                    bt = BarcodeType.EAN128B;
                    break;
                case "EAN128C":
                    bt = BarcodeType.EAN128C;
                    break;
                case "EAN13":
                    bt = BarcodeType.EAN13;
                    break;
                case "EAN13_2":
                    bt = BarcodeType.EAN13_2;
                    break;
                case "EAN13_5":
                    bt = BarcodeType.EAN13_5;
                    break;
                case "EAN8":
                    bt = BarcodeType.EAN8;
                    break;
                case "EAN8_2":
                    bt = BarcodeType.EAN8_2;
                    break;
                case "EAN8_5":
                    bt = BarcodeType.EAN8_5;
                    break;
                case "INTERLEAVED2OF5":
                    bt = BarcodeType.INTERLEAVED2OF5;
                    break;
                case "MSIPLESSEY":
                    bt = BarcodeType.MSIPLESSEY;
                    break;
                case "MSIPLESSEYCHECK10":
                    bt = BarcodeType.MSIPLESSEYCHECK10;
                    break;
                case "MSIPLESSEYCHECK1010":
                    bt = BarcodeType.MSIPLESSEYCHECK1010;
                    break;
                case "MSIPLESSEYCHECK11":
                    bt = BarcodeType.MSIPLESSEYCHECK11;
                    break;
                case "MSIPLESSEYCHECK1110":
                    bt = BarcodeType.MSIPLESSEYCHECK1110;
                    break;
                case "PLANET":
                    bt = BarcodeType.PLANET;
                    break;
                case "POSTNET":
                    bt = BarcodeType.POSTNET;
                    break;
                case "ROYALMAIL":
                    bt = BarcodeType.ROYALMAIL;
                    break;
                case "UPCA":
                    bt = BarcodeType.UPCA;
                    break;
                case "UPCA_2":
                    bt = BarcodeType.UPCA_2;
                    break;
                case "UPCA_5":
                    bt = BarcodeType.UPCA_5;
                    break;
                case "UPCE":
                    bt = BarcodeType.UPCE;
                    break;
                case "UPCE_2":
                    bt = BarcodeType.UPCE_2;
                    break;
                case "UPCE_5":
                    bt = BarcodeType.UPCE_5;
                    break;
            }
            return bt;
        }

        private void cheCode39_CheckedChanged(object sender, EventArgs e)
        {
            barcodeControl2.ShowCode39StartStop = cheCode39.Checked;
            Invalidate();
        }

        private void txtData_TextChanged(object sender, EventArgs e)
        {
           // barcodeControl2.Data = txtData.Text;
            //Invalidate();
        }

        private void txtSubData_TextChanged(object sender, EventArgs e)
        {
            barcodeControl2.AddOnData = txtSubData.Text;
            Invalidate();
        }

        private void comDataShow_SelectedIndexChanged(object sender, EventArgs e)
        {
            barcodeControl2.TextPosition = GetTextPosition(comDataShow.Text);
            Invalidate();
        }

        BarcodeTextPosition GetTextPosition(string str)
        {
            switch (str)
            {
                case "顶部":
                    return BarcodeTextPosition.Above;
                case "不显示":
                    return BarcodeTextPosition.NotShown;
                case "底部":
                default:
                    return BarcodeTextPosition.Below;
            }
        }

        private void BarCodeSetupBox_Load(object sender, EventArgs e)
        {
            comType.SelectedIndex = 0;
            comDataShow.SelectedIndex = 0;
            Invalidate();
        }

        private void vistaButton1_Click(object sender, EventArgs e)
        {
            try
            {
                startI = 0;
                pd.PrintPage -= new PrintPageEventHandler(pd_PrintPage);
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                PrintPreviewDialog cppd = new PrintPreviewDialog();
                cppd.Document = pd;
                cppd.ShowDialog();
            }
            catch
            {
                MessageBox.Show("数据有错误，或打印机出错！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void GetFormData()
        {
            this.bx = (int)numericUpDown2.Value;
            this.by = (int)numericUpDown3.Value;
            if (this.txtLx.Text.Trim()!="")
                this.lx = Convert.ToInt32(this.txtLx.Text.Trim());
            if (this.txtly.Text.Trim() != "")
                this.ly = Convert.ToInt32(this.txtly.Text.Trim());
            if (this.txtData.Text.Trim() != "")
                this.barData = Convert.ToInt32(this.txtData.Text.Trim());
            this.barFont = this.txtFontBar.Text.Trim();
            this.barBack = this.txtBakBar.Text.Trim();
            if (this.txtLie.Text.Trim() != "")
                this.barLie = Convert.ToInt32(this.txtLie.Text);
            if (this.txtNum.Text.Trim() != "")
                this.barNum = Convert.ToInt32(this.txtNum.Text.Trim());
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            GetFormData();

            //int PageHeight;
          
            //PageHeight = pd.DefaultPageSettings.PaperSize.Height;
            
            Graphics g = e.Graphics;
            
            int barlen = 10;
            string bar="";
            int mbar;
            int i;
            int x=0,y=0;
            int begin = 0;
           
            for (i = startI; i < barNum; i++)
            {
                bar = "";
                bar += barFont;
                barlen = 10;
                mbar = this.barData + i;
                barlen = barlen - barFont.Length - barBack.Length;
                barlen = barlen-mbar.ToString().Length;
                for (int j = 0; j < barlen; j++)
                    bar += "0";
                bar += mbar.ToString();
                if (this.barBack != null)
                    bar += barBack;
                Rectangle rect = barcodeControl2.ClientRectangle;
                //下面是调整打印位置的代码。
                if (begin < this.barLie)
                {
                    if (begin == 0)
                    {
                        y = y + by;
                        x = bx;
                    }
                    else
                    {
                        x = x + bx + lx;
                    }
                }
                else
                {
                    if (i % this.barLie == 0)
                    {
                        x = bx;
                        y = y + by + ly;

                    }
                    else
                    {
                        x = x + bx + lx;
                    }
                }

                barcodeControl2.Data = bar;
                ii++;
                begin++;
                rect = new Rectangle(rect.X + x, rect.Y + y, rect.Width, rect.Height);
                //rect1 = new Rectangle(rect.X + 500, rect.Y + 500, rect.Width, rect.Height);
                //打印
                barcodeControl2.Draw(g, rect, GraphicsUnit.Inch, 0.01f, 0, null);
                if ((y +by+ly+by+ly)> e.PageSettings.PaperSize.Height && ((i+1)%this.barLie==0))
                {
                    g.Dispose();
                    startI = i+1;
                    e.HasMorePages = true;
                    return;
                }
                if (i == (barNum-1))
                {
                    g.Dispose();
                    e.HasMorePages = false;
                    return;
                }
           
            }
           
            g.Dispose();
            
           
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            barcodeControl2.StretchText = checkBox1.Checked;
        }

        void txtTitle_TextChanged(object sender, System.EventArgs e)
        {
            barcodeControl2.CopyRight = txtTitle.Text;
        }

        private void vistaButton3_Click(object sender, EventArgs e)
        {
            System.IO.Stream myStream;
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "GIF 文件 (*.gif)|*.gif|PNG 文件 (*.png)|*.png|JPEG 文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg|BMP 文件 (*.bmp;*.dib)|*.bmp;*.dib|TIFF 文件 (*.tif;*.tiff)|*.tif;*.tiff";
            fileDialog.FilterIndex = 1;
            fileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            fileDialog.RestoreDirectory = false;
            ImageFormat[] afmt = { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Tiff };

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = fileDialog.OpenFile()) != null)
                {
                    int barcodeWidth = 1;
                    int barcodeHeight = 50;
                    barcodeControl2.MakeImage(afmt[fileDialog.FilterIndex - 1], barcodeWidth, barcodeHeight, true, false, null, myStream);
                    myStream.Close();
                }
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
        }

        private void vistaButton2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                
            }
            catch { }
        }

        private void txtLie_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                PageSetupDialog fm = new PageSetupDialog();
                fm.Document = pd;
                fm.ShowDialog();
            }
            catch
            {
                MessageBox.Show("数据有错误，或打印机出错！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}